#include "stdafx.h"
#include "MenuState.h"
#include "GameGUI.h"
#include "RenderSystem.h"
#include "SceneManager.h"
#include "CameraControl.h"
#include "Skydome.h"
#include "InputManager.h"

MenuState::MenuState(void){}
MenuState::~MenuState(void){}

void MenuState::Init(){
	const float size = 0.25f;
	float x = static_cast<float>(m_systems->m_render_system->GetWidth())/ 2.0f;
	float y = static_cast<float>(m_systems->m_render_system->GetHeight()) / 2.0f;
	float w = x * size;
	float h = y * size;
	float w_offset = w * 0.5f;
	float h_offset = h * 0.5f;
	int x_pos = static_cast<int>(x - w_offset);
	int y_pos = static_cast<int>(y - h_offset);

	m_render_system = m_systems->m_render_system;

	m_game_gui = new GameGUI(m_systems->m_gui_manager);
	
	m_game_gui->CreateButton(Gui::EButtonType::START_GAME, x_pos, y_pos, static_cast<int>(w), static_cast<int>(h), "Start Game", 0x00000000, 7);
	m_game_gui->CreateButton(Gui::EButtonType::QUIT_GAME, x_pos, y_pos + 200, static_cast<int>(w), static_cast<int>(h), "Quit Game", 0x00000000, 7);

	m_game_gui->GetButton(Gui::EButtonType::START_GAME)->SetTextPosition(Vector2(30, 40));
	m_game_gui->GetButton(Gui::EButtonType::QUIT_GAME)->SetTextPosition(Vector2(30, 40));

	m_game_gui->CreateTextfield(
		ETextfield_Type::TITLE,
		(m_render_system->GetWidth() / 2) - 200, 
		200,
		0.28f * m_render_system->GetWidth(), 
		0.18f * m_render_system->GetHeight(), 
		"TITLE", 
		0x00000000, 
		24, 
		true);
	Gui::Textfield* field = m_game_gui->GetTextfield(ETextfield_Type::TITLE);
	field->SetTextPosition(Vector2(40, 100));
	field->SetTexcoords(0.0f, (float)15/(float)16, (float)6/(float)16, (float)1/(float)16);

	float width = (float)m_systems->m_render_system->GetWidth();
	float height = (float)m_systems->m_render_system->GetHeight();

	m_camera = m_systems->m_camera;
	//m_camera->SetPerspective(width/height,DirectX::XM_PIDIV4,m_systems->m_config_file->AsFloat("c_znear"),m_systems->m_config_file->AsFloat("c_zfar"));
	m_camera_control = new CameraControl(m_camera);
	m_camera_control->Init(m_systems->m_message_system);
	m_camera_control->Settings(m_systems->m_config_file->AsFloat("c_speed"),m_systems->m_config_file->AsFloat("m_yaw"),m_systems->m_config_file->AsFloat("m_pitch"),m_systems->m_config_file->AsFloat("m_sensitivity"));

	Skydome *skydome=new Skydome;
	skydome->Init(m_systems->m_render_system);
	m_systems->m_scene_manager->SetSkydome(skydome);

	m_menu_song.Load("../data/audio/menu_song.ogg");
	m_menu_song.SetLoop(true);
	m_menu_song.SetVolume(0.5f);
	m_menu_song.Play();
}

void MenuState::Shut(){
	
	m_camera_control->Shut(m_systems->m_message_system);
	delete m_camera_control;
	m_camera_control = nullptr;
	m_systems->m_scene_manager->Shut();

	m_systems->m_gui_manager->ClearAllWidgets();
	delete m_game_gui;
	m_game_gui = nullptr;
}

void MenuState::Update(float deltatime){
	m_camera_control->Update(deltatime);
	m_systems->m_gui_manager->Update();

	Vec2 mouse = m_systems->m_input_manager->m_mouse.m_position;

	Gui::Button* button = m_game_gui->GetButton(mouse.x, mouse.y);
	if (m_systems->m_input_manager->ButtonPressed(EMouseButton::MB_Left) && button != nullptr){
		switch (button->GetButtonType())
		{
		case Gui::EButtonType::START_GAME:
			{
				m_menu_song.Pause();
				ChangeState(FindById("GameState"));
				break;
			}
		case Gui::EButtonType::QUIT_GAME:
			{
				m_menu_song.Pause();
				Shutdown();
				break;
			}
		default:
			break;
		}
	}

}

void MenuState::Render(){
	m_systems->m_render_system->Clear();
	m_systems->m_scene_manager->Render(m_systems->m_render_system, m_camera);
	m_systems->m_gui_manager->Render();
	m_systems->m_render_system->Present();
}
